<template>
  <div class="container-content">
    <div class="app-view-box">
      <el-row>
        <el-col :span="24">
          <h1 class="panel-title">应付票据结算单</h1>
        </el-col>
      </el-row>
      <el-row style="margin: 10px 0" justify="end" class="export-container">
        <el-col :span="1.5" style="margin-right: 10px">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="add"
            >新增
          </el-button>
        </el-col>
        <el-col :span="8">
          <el-input
            v-model="search_value"
            placeholder="请输入搜索内容"
            clearable
            size="small"
            @keyup.enter.native="getData"
          />
          <el-button
            style="margin-left: 5px"
            icon="el-icon-search"
            size="mini"
            type="primary"
            @click="getData"
            >搜索</el-button
          >
          <el-button size="mini" @click="reset">重置</el-button>
        </el-col>
      </el-row>
      <div class="container_table">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            type="index"
            width="50"
            label="序号"
          ></el-table-column>
          <el-table-column prop="billSerialNo" label="票据流水号">
          </el-table-column>
          <el-table-column prop="billType" label="票据类型"> </el-table-column>
          <el-table-column prop="billNo" label="票据号"> </el-table-column>
          <el-table-column prop="kingBaseCurrencyId" label="币别">
            <template slot-scope="scope">
              {{ handleCurrency(scope.row.kingBaseCurrencyId) }}
            </template>
          </el-table-column>
          <el-table-column prop="receiptDeptId" label="收款单位">
          </el-table-column>
          <el-table-column prop="dueTime" label="到期日"> </el-table-column>
          <el-table-column prop="faceAmount" label="票面金额">
          </el-table-column>
          <el-table-column prop="acceptanceBankId" label="承兑人">
          </el-table-column>
          <el-table-column prop="settlementStatus" label="结算状态">
            <template slot-scope="scope">
              {{ handleSettlementStatus(scope.row.settlementStatus) }}
            </template>
          </el-table-column>
          <el-table-column prop="dataStatus" label="数据类型">
            <template slot-scope="scope">
              {{ handleDataStatus(scope.row.dataStatus) }}
            </template>
          </el-table-column>
          <el-table-column prop="payBankName" label="付款组织">
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="详情"
                placement="bottom"
              >
                <el-button
                  plain
                  icon="el-icon-more"
                  @click="detail(scope.row)"
                  size="mini"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="编辑"
                placement="bottom"
              >
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-edit"
                  @click="edit(scope.row)"
                  size="mini"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="删除"
                placement="bottom"
              >
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  @click="del(scope.row)"
                  size="mini"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-pagination
      class="table-pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      direction="rtl"
      size="100%"
      destroy-on-close
    >
      <detail v-if="drawerType === 'detail'" :rowId="rowId" />
    </el-drawer>
    <el-dialog
      :title="title"
      :visible.sync="dialog"
      :before-close="handleClose"
      width="60%"
      destroy-on-close
    >
      <addEdit
        ref="addEdit"
        v-if="drawerType === 'add' || drawerType === 'edit'"
        :rowId="rowId"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="submit">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, deleteInfo } from "@/api/finance/statementPay";
import {
  handleDataStatus,
  handleSettlementStatus,
  handleCurrency,
} from "../common";
import addEdit from "./addEdit";
import detail from "./detail";

export default {
  components: { addEdit, detail },
  data() {
    return {
      search_value: "", //搜索值
      currentPage: 1, //当前页
      pageSize: 10, //每页条数
      total: 0,
      tableData: [],
      rowId: "",
      payId: "",
      drawerType: "",
      drawer: false,
      dialog: false,
      rowIdList: [],
      kingBaseCurrencyIdOptions: [],
    };
  },
  computed: {
    title() {
      if (this.drawerType === "detail") {
        return "应付票据结算单-详情";
      } else if (this.drawerType === "add") {
        return "应付票据结算单-新增";
      } else if (this.drawerType === "edit") {
        return "应付票据结算单-编辑";
      }
    },
  },
  methods: {
    //获取list
    async getData() {
      const query = {
        limit: this.pageSize,
        page: this.currentPage,
      };
      let res = await getList(query);
      if (res.code === 200) {
        this.tableData = res.rows;
        this.total = res.total;
      }
    },
    //分页器size
    handleSizeChange(val) {
      this.pageSize = val;
      this.getDataList();
    },
    //分页器当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDataList();
    },
    add(row) {
      this.drawerType = "add";
      this.rowId = "";
      this.dialog = true;
    },
    edit(row) {
      this.drawerType = "edit";
      this.rowId = row.id;
      this.dialog = true;
    },
    detail(row) {
      this.drawerType = "detail";
      this.rowId = row.id;
      this.drawer = true;
    },
    del(row) {
      this.$confirm("请确定要删除所选的记录吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteInfo(row.id).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //重置
    reset() {
      this.search_value = "";
      this.getData();
    },
    //关闭drawer
    handleClose(done) {
      this.$confirm("确定要离开吗？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    submit() {
      const cb = () => {
        this.drawer = false;
        this.getData();
      };
      this.$refs.addEdit.submit(cb);
    },
    //取消drawer
    cancelForm() {
      this.drawer = false;
      this.rowId = "";
      this.getData();
    },
    // 导出按钮
    export() {
      console.log("exporting");
    },

    handleDataStatus(type) {
      return handleDataStatus(type);
    },
    handleSettlementStatus(type) {
      return handleSettlementStatus(type);
    },
    handleCurrency(type) {
      return handleCurrency(this.kingBaseCurrencyIdOptions, type);
    },
  },

  mounted() {
    this.getDicts("king_currency_type").then((res) => {
      this.kingBaseCurrencyIdOptions = res.data.map((item) => {
        return {
          label: item.dictLabel,
          value: item.dictValue,
        };
      });
    });
    this.getData();
  },
};
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  padding: 5px 10px;
  .container_title {
    border-bottom: 1px solid #ccc;
    padding: 10px 0;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .container_title_left {
      display: flex;
      align-items: center;
    }
    .container_title_right {
      .add_btn {
        width: 100px;
      }
    }
  }
  .container_table {
    margin-top: 10px;
  }
}
</style>
